//AVISplitter.h
#include <Dshow.h>
#include <dmodshow.h>
#include "..\WMP\List.h"

#pragma once

class AVISplitterFactory: public IClassFactory {
	IClassFactory *factory;

	AVISplitterFactory(IClassFactory *factory);
	~AVISplitterFactory();
public:
	static HRESULT Create(IClassFactory *factory, REFIID riid, void** ppv);

	/*IUnknown*/
	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject);
	ULONG STDMETHODCALLTYPE AddRef();
	ULONG STDMETHODCALLTYPE Release();

	/*IClassFactory*/
	HRESULT STDMETHODCALLTYPE CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject);
	HRESULT STDMETHODCALLTYPE LockServer(BOOL fLock);
};

class AVISplitter: public IAMStreamSelect {
	IUnknown *inner;
	IBaseFilter *splitter;
	IFilterGraph *graph;
	ULONG ref;

	class PinInfo {
	public:
		long index;
		IPin* pin;
		IPin* connect;
		IBaseFilter* filter;
		WCHAR filterName[MAX_FILTER_NAME];
		AM_MEDIA_TYPE* type;
		PinInfo* next;
		PinInfo* CreateNext();
		PinInfo(long index, IPin* pin, AM_MEDIA_TYPE* type, bool chain = true);
		~PinInfo();
	};

	CArray<PinInfo*>* streams;

	AVISplitter();
	~AVISplitter();
	HRESULT EnsurePins();
	HRESULT EnsureGraph();
public:
	static HRESULT Create(IClassFactory *factory, void** ppvObject);

	/*IUnknown*/
	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject);
	ULONG STDMETHODCALLTYPE AddRef();
	ULONG STDMETHODCALLTYPE Release();

	/*IAMStreamSelect*/
	HRESULT STDMETHODCALLTYPE Count(DWORD *pcStreams);
	HRESULT STDMETHODCALLTYPE Info(long lIndex, AM_MEDIA_TYPE **ppmt, DWORD *pdwFlags, LCID *plcid, DWORD *pdwGroup, LPWSTR *ppszName, IUnknown **ppObject, IUnknown **ppUnk);
	HRESULT STDMETHODCALLTYPE Enable(long lIndex, DWORD dwFlags);
};

